package demo1;

//装饰模式 动态的给一个对象添加一些额外的职责 ，就增加功能来说,装饰模式比生成子类更为灵活

public class Main {
    public static void main(String[] args) {
        Component c = new ConcreteComponent();
        Decorator d1 = new ConcreteDecoratorA();
        Decorator d2 = new ConcreteDecoratorB();

        d1.setComponent(c);
        d2.setComponent(d1);
        d2.Operation();

    }
}

abstract class Component{
    public abstract void Operation();
}

class ConcreteComponent extends Component{
    @Override
    public void Operation() {
        System.out.println("对象的具体操作");
    }
}

abstract class Decorator extends Component{
    protected Component component;

    public void setComponent(Component component) {
        this.component = component;
    }

    @Override
    public void Operation() {
        if(component != null){
            component.Operation();
        }
    }
}

class ConcreteDecoratorA extends Decorator{
    private String addedState; // 本类独有的功能，以区别 ConcreteDecoratorB

    @Override
    public void Operation() {
        super.Operation();
        addedState = "New State";
        System.out.println("具体装饰对象A的操作" +addedState);
    }
}
class ConcreteDecoratorB extends Decorator{
    @Override
    public void setComponent(Component component) {
        super.setComponent(component);
        AddedBehavior();
        System.out.println("具体装饰对象B的操作");
    }
    private void AddedBehavior(){
        System.out.println("B类特有方法");
    }
}